import UserData from "./data/UserData";

export default class DataManager{
    private static _ins:DataManager;
    public static get ins():DataManager{
        if(!this._ins)this._ins=new DataManager();
        return this._ins;
    }

    public user:UserData;
    private mGameCfg=[];
    private mMaxGameIndex:number=0;
    
    private mSpGameCfg=[];
    constructor(){
        this.user = new UserData();
        this.updateUserDataFronLocal();
    }

    public setGameCfg(data){
        this.mGameCfg=data;
        this.mMaxGameIndex=this.mGameCfg.length;
    }

    public getCurrGateCfg(){
        return this.getGateCfg(this.user.currGate);
    }

    public getGateCfg(index:number){
        if(this.mGameCfg)return this.mGameCfg[index];
        return null;
    }

    public nextGate(){
        if(this.user.currGate<this.mMaxGameIndex-1){
            this.user.currGate++;
        }
    }

    public setSpGameCfg(data){
        this.mSpGameCfg=data;
    }

    public getRandomSpGateCfg(){
        if(this.mSpGameCfg && this.mSpGameCfg.length){
            return this.mSpGameCfg[Math.floor(Math.random()*this.mSpGameCfg.length)];
        }
        return null;
    }

    public getMapCfg(index:number){
        return null;
    }

    public isMaxMap(index:number){
        return false;
    }

    public saveUserData(){
        let tdata = this.user.getSaveData();
        localStorage.setItem("user_data",JSON.stringify(tdata));
    }

    public updateUserDataFronLocal(){
        let str = localStorage.getItem("user_data");
        if(str){
            this.user.parseFromLocal(JSON.parse(str));
        }
    }
}